/*
 * Register Logical Form. 
 */
package rai.form;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;

import rai.blo.RegFormBLO;
import rai.util.Constants;

/**
 * @author Vu
 * 
 */
public class RegForm {

    private String txtUserName = "";
    private String txtPassword = "";
    private String txtEmpName = "";
    private String txtAddress = "";
    private String txtBirthday = "";
    private String txtPhone = "";
    private String txtEmail = "";
    private int radioGender = 0;
    private int lstDept = 0;

    public String getUserName() {
        return txtUserName;
    }

    public String getPassword() {
        return txtPassword;
    }

    public void setUserName(String name) {
        this.txtUserName = name;
    }

    public void setPassword(String pass) {
        this.txtPassword = pass;
    }

    private static Logger log = Logger.getLogger("RegisterForm");

    public boolean checkID(String ID) {
        boolean chk = true;
        for (int i = 0; i < ID.length(); i++) {
            if (ID.charAt(i) < 48 || ID.charAt(i) > 57) {
                chk = false;
                break;
            }
        }
        return chk;
    }

    public List<String> validate(RenderRequest request, RenderResponse response) {
        log.debug("validate.start");
        List<String> errorMess = new ArrayList<String>();
        RegFormBLO regBlo = new RegFormBLO();

        if (this.txtUserName.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input the Username!");
            log.debug("validate.errorMess: Please input the Username!");
        } else if (regBlo.checkUsername(this.txtUserName) != null) {
            request.getPortletSession().setAttribute(
                    Constants.NAME_PORT_SESS_ATT, this.txtUserName,
                    PortletSession.APPLICATION_SCOPE);
            log
                    .debug("validate.errorMess: Username does not match the requirement!");
            errorMess.add(regBlo.checkUsername(this.txtUserName));
        } else {
            request.getPortletSession().setAttribute(
                    Constants.NAME_PORT_SESS_ATT, this.txtUserName,
                    PortletSession.APPLICATION_SCOPE);
        }

        if (this.txtPassword.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input the Password!");
            log.debug("validate.errorMess: Please input the Password!");
        } else if (regBlo.checkPass(txtPassword) != null) {
            errorMess.add(regBlo.checkPass(txtPassword));
            log
                    .debug("validate.errorMess: Password does not match the requirement!");
        }
        log.debug("validateRegister.end");

        return errorMess;
    }

    /**
     * @param txtEmpName
     *            the txtEmpName to set
     */
    public void setTxtEmpName(String txtEmpName) {
        this.txtEmpName = txtEmpName;
    }

    /**
     * @return the txtEmpName
     */
    public String getTxtEmpName() {
        return txtEmpName;
    }

    /**
     * @param txtAddress
     *            the txtAddress to set
     */
    public void setTxtAddress(String txtAddress) {
        this.txtAddress = txtAddress;
    }

    /**
     * @return the txtAddress
     */
    public String getTxtAddress() {
        return txtAddress;
    }

    /**
     * @param txtBirthday
     *            the txtBirthday to set
     */
    public void setTxtBirthday(String txtBirthday) {
        this.txtBirthday = txtBirthday;
    }

    /**
     * @return the txtBirthday
     */
    public String getTxtBirthday() {
        return txtBirthday;
    }

    /**
     * @param txtPhone
     *            the txtPhone to set
     */
    public void setTxtPhone(String txtPhone) {
        this.txtPhone = txtPhone;
    }

    /**
     * @return the txtPhone
     */
    public String getTxtPhone() {
        return txtPhone;
    }

    /**
     * @param txtEmail
     *            the txtEmail to set
     */
    public void setTxtEmail(String txtEmail) {
        this.txtEmail = txtEmail;
    }

    /**
     * @return the txtEmail
     */
    public String getTxtEmail() {
        return txtEmail;
    }

    /**
     * @param radioGender
     *            the radioGender to set
     */
    public void setRadioGender(int radioGender) {
        this.radioGender = radioGender;
    }

    /**
     * @return the radioGender
     */
    public int getRadioGender() {
        return radioGender;
    }

    /**
     * @param lstDept
     *            the lstDept to set
     */
    public void setLstDept(int lstDept) {
        this.lstDept = lstDept;
    }

    /**
     * @return the lstDept
     */
    public int getLstDept() {
        return lstDept;
    }
    
    /**
     * check Email.
     * @param email
     * @return String
     */
    public String checkEmail(final String email) {
        final String error = null;
        // check if '@' is at the first position or at last position or absent
        // in given email
        if (email.charAt(0) == '@' || email.charAt(email.length() - 1) == '@'
                || email.indexOf("@") == -1) {
            return "Invalid Email Address1";
        }
        // check if '.' is at the first position or at last position or absent
        // in given email
        if (email.charAt(0) == '.' || email.charAt(email.length() - 1) == '.'
                || email.indexOf(".") == email.length()) {
            return "Invalid Email Address2";
        }
        // check if '@' is used more than one times in given email
        if (email.indexOf("@", email.indexOf("@") + 1) != -1) {
            return "Invalid Email Address3";
        }
        // check for the position of '.'
        if (email.indexOf(".") - email.indexOf("@") <= 2) {
            return "Invalid Email Address4";
        }
        // check for blank spaces in given email
        if (email.indexOf(" ") != -1) {
            return "Invalid Email Address6";
        }
        return error;
    }

    /**
     * Check Name.
     * @param string
     * @return boolean
     */
    public boolean checkName(final String string) {
        log.debug("checkName.start");
        boolean check = true;
        if (string.charAt(0) == ' '
                || string.charAt(string.length() - 1) == ' ') {

             check = false;
        } else {
            for (int i = 0; i < string.length(); i++) {
                if (string.charAt(i) >= 'A' && string.charAt(i) <= 'Z') {
                    continue;
                } else if (string.charAt(i) >= 'a' && string.charAt(i) <= 'z') {
                    continue;
                } else if (string.charAt(i) == ' ' && string.charAt(i + 1) != ' ') {
                    continue;
                } else {
                    check = false;
                    break;
                }
            }
        }
        log.debug("checkName.end." + check);
        return check;
    }

    /**
     * Check Address.
     * @param string
     * @return boolean
     */
    public boolean checkAddress(final String string) {
        boolean check = true;
        if (string.charAt(0) == ' '
                || string.charAt(string.length() - 1) == ' ') {
            check = false;
        } else {
            for (int i = 0; i < string.length(); i++) {
                if (string.charAt(i) >= '0' && string.charAt(i) <= '9') {
                    continue;
                } else if (string.charAt(i) >= 'A' && string.charAt(i) <= 'Z') {
                    continue;
                } else if (string.charAt(i) >= 'a' && string.charAt(i) <= 'z') {
                    continue;
                } else if (string.charAt(i) == '/' || string.charAt(i) == '-'
                        || string.charAt(i) == '.') {
                    continue;
                } else if (string.charAt(i) == ' ' && string.charAt(i + 1) != ' ') {
                    continue;
                } else {
                    check = false;
                    break;
                }
            }
        }
        log.debug("checkAddress.end." + check);
        return check;
    }

    /**
     * Check Phone.
     * @param phone
     * @return
     */
    public boolean checkPhone(final String phone) {
        for (int i = 0; i < phone.length(); i++) {
            if (phone.charAt(i) == '(') {
                continue;
            } else if (phone.charAt(i) >= '0' && phone.charAt(i) <= '9') {
                continue;
            } else if (phone.charAt(i) != ')') {
                continue;
            } else if (phone.charAt(i) != '.') {
                continue;
            } else {
                return false;
            }
        }
        if (phone.length() == 12) {
            if (phone.charAt(3) != '.') {
                return false;
            } else if (phone.charAt(7) != '.')
                return false;
        } else if (phone.length() == 13) {
            if (phone.charAt(4) != '.') {
                return false;
            } else if (phone.charAt(8) != '.') {
                return false;
            }
        } else {
            return false;
        }
        log.debug("checkPhone.end.");
        return true;
    }
    /**
     * Validate EmpInfo.
     * @param request
     * @param response
     * @return
     */
    public List<String> validateEditEmpInfo(final RenderRequest request,
            final RenderResponse response) {
        log.debug("validateEditEmpInfo.start");
        final List<String> errorMess = new ArrayList<String>();
        // check Employee Name
        if (this.txtEmpName.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input Employee Name!");
            log.debug("validate.errorMess: Please input Emp Name!");
        } else if (this.txtEmpName.length() < 3) {
            errorMess.add("Name must has at least 3 letters!");
            log.debug("validate.errorMess:Name must has at least 3 letters!");
        } else if (!checkName(txtEmpName)) {
            errorMess
                    .add("Name only contains letters, not start with a blank and every part seperated by a blank!");
            log
                    .debug("validate.errorMess:Name only contains letters, not start with a blank and every part "
                            + "seperated by a blank!");
        }
        request.getPortletSession().setAttribute("regEmpName", txtEmpName,
                PortletSession.APPLICATION_SCOPE);
        // check Address
        if (this.txtAddress.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input the Address!");
            log.debug("validate.errorMess: Please input the Address!");
        } else if (this.txtAddress.length() < 10) {
            errorMess.add("Address must has at least 10 letters!");
            log
                    .debug("validate.errorMess:Address must has at least 10 letters!");
        } else if (!checkAddress(txtAddress)) {
            errorMess
                    .add("Address only contains letters and numbers,not start with a blank and separated by a blank!");
            log
                    .debug("validate.errorMess:Address only contains letters and numbers,not start with a blank "
                            + "and separated by a blank!");
        }
        request.getPortletSession().setAttribute("regAddress", txtAddress,
                PortletSession.APPLICATION_SCOPE);
        // check Email
        if (this.txtEmail.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input the Email Address!");
            log.debug("validate.errorMess:Please input the Email Address!");
        } else if (checkEmail(txtEmail) != null) {
            errorMess.add(checkEmail(txtEmail));
            log.debug("validate.errorMess:" + checkEmail(txtEmail));
        }
        request.getPortletSession().setAttribute("regEmail", txtEmail,
                PortletSession.APPLICATION_SCOPE);
        // check Phone
        if (this.txtPhone.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input Phone Number!");
            log.debug("validate.errorMess: Please input Phone Number!");
        } else if (!checkPhone(txtPhone)) {
            errorMess
                    .add("Invalid Phone Number. Phone has format such as: 0123.456.7890!");
            log
                    .debug("validate.errorMess: Invalid Phone Number.Phone has format such as: 0123.456.7890!");
        }
        request.getPortletSession().setAttribute("regPhone", txtPhone,
                PortletSession.APPLICATION_SCOPE);
        log.debug("validateEditEmpInfo.end");
        return errorMess;
    }
}
